package com.guojian.common.http;

import java.io.IOException;
import java.nio.channels.SocketChannel;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Base64Utils;

import com.google.common.base.Charsets;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.NioUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.server.HttpServerRequest;
import cn.hutool.http.server.HttpServerResponse;
import cn.hutool.http.server.SimpleServer;
import cn.hutool.http.server.action.Action;
import cn.hutool.socket.nio.ChannelHandler;
import cn.hutool.socket.nio.NioServer;

public class Test {

	public static void main(String[] args) {

		/*Map<String, String> abc = new HashMap<String, String>();
		abc.put("key", "name");
		
		Response response = HttpUtils.post("http://localhost:9081/config1", abc);//128.1.108.215:8079
		
		System.out.println(response.toString());*/

		//nioServer();

		//split();

		

	}

	public static void split() {
		String[] ss = StringUtils.splitByWholeSeparator("@|6|0||成功|9876543210||#", "|");
		ss = StrUtil.split("|@|6|0||成功|9876543210||#", "|");
		for (String string : ss) {
			System.out.println(string);
		}
		byte[] aa = new byte[] {};
		System.out.println(new String(aa));
	}

	public static void nioServer() {
		NioServer nioServer = new NioServer(9001);

		nioServer.setChannelHandler(new ChannelHandler() {
			@Override
			public void handle(SocketChannel socketChannel) throws Exception {
				FastByteArrayOutputStream outStream = NioUtil.read(socketChannel);
				System.out.println(new String(outStream.toByteArray()));
				socketChannel.close();
			}
		});

		nioServer.start();

		nioServer.close();

	}

	public void createHttpServer() {
		SimpleServer server = new SimpleServer(9080);
		server.addAction("guojian/a", new Action() {
			@Override
			public void doAction(HttpServerRequest request, HttpServerResponse response) throws IOException {
				response.setContentType(ContentType.build(ContentType.TEXT_HTML.getValue(), Charsets.UTF_8));
				response.write("成功执行OK");
			}
		});
		server.start();
	}

}
